//
//  GupiaoAPI.swift
//  QMXK
//
//  Created by jenkin on 2018/12/26.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import Foundation
import Moya

private func JSONResponseDataFormatter(_ data: Data) -> Data {
    do {
        let dataAsJSON = try JSONSerialization.jsonObject(with: data)
        let prettyData =  try JSONSerialization.data(withJSONObject: dataAsJSON, options: .prettyPrinted)
        return prettyData
    } catch {
        return data // fallback to original data if it can't be serialized.
    }
}

// MARK: - Provider support
private extension String{
    var urlEscaped:String{
        return self.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
    }
}

public enum GupiaoAPI{
    case getGP(data:[String:Any])
    case getGPDetail(data:[String:Any])
}

//http://localhost:8888/tp5/public/index/gp/index
//http://localhost:8888/tp5/public/index/gpdata/index

extension GupiaoAPI: TargetType {
    
    public var method: Moya.Method {
        return .get
    }
    
    public var sampleData: Data {
        return Data(base64Encoded: "just for test")!
    }
    
    public var task: Task {
        var newData:[String:Any]
        switch self {
        case let .getGP(data):
            newData = data
            break;
        case let .getGPDetail(data):
            newData = data
            break;
        }
        return .requestParameters(parameters: newData, encoding: URLEncoding.default)
    }
    
    public var headers: [String : String]? {
        return ["Content-type" : "application/json"]
    }
    
    public var baseURL: URL{
        let url:String = String.init(format: "http://%@/tp5/public/index/",ZZBaseUtil.shareUtil.getIpHost())
        return URL(string: url)!
        //return URL(string: "http://192.168.1.158:8888/tp5/public/index/")!
    }
    
    public var path: String{
        switch self {
        case .getGP:
            return "gp/index"
        case .getGPDetail:
            return "gpdata/index"
        }
    }
}
